package com.sfd.test.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sfd.core.entity.Stock;
import com.sfd.core.entity.User;
import com.sfd.test.mapper.StockMapper;
import com.sfd.test.service.StockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author haden
 * @date 2022/8/8 18:42
 **/
@Slf4j
@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements StockService {

    @Override
    public void sub(Long id, Integer num) {
        Stock stock = getById(id);
        if (stock == null) {
            throw new RuntimeException("stock c not b fount");
        }
        int newNum = stock.getNum() - num;
        stock.setNum(newNum);
        updateById(stock);
        if (newNum < 0) {
            throw new RuntimeException("stock number not enough");
        }
    }
}
